/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年4月14日 上午8:33:06
 */
package com.lenovo.lmrp.server.itsmgateway.adapter.ms;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.lenovo.lmrp.api.model.entity.ProjectEngineerPO;
import com.lenovo.lmrp.server.itsmgateway.domain.metadata.ItsmEntityTranslator;
import com.lenovo.lmrp.server.itsmgateway.domain.metadata.ItsmEntityTranslator.EntityName;
import com.lenovo.lmrp.server.itsmgateway.repository.ARepositoryFactory;
import com.lenovo.sdc.framework.util.CommonUtil;

/**
 * 
 * @author wujin
 * @version v1.0
 */
public class BaseDataImporter {

    private static final Map<String, String> enumMap = new HashMap<>();

    static {
        enumMap.put("可接单", "0");
        enumMap.put("不可接单-系统示忙", "1");
        enumMap.put("不可接单-忙碌", "2");
        enumMap.put("不可接单-休假", "3");
        enumMap.put("不可接单-病假", "4");
        enumMap.put("不可接单-培训", "5");
        enumMap.put("离岗", "6");
        enumMap.put("锁定工程师", "0");
        enumMap.put("备份工程师", "1");
        enumMap.put("买断工程师", "2");
        enumMap.put("包店工程师", "3");
        enumMap.put("承包工程师", "4");
        enumMap.put("门店类", "0");
        enumMap.put("桌面类", "1");
        enumMap.put("网络类", "2");
        enumMap.put("连锁类", "3");
        enumMap.put("否", "0");
        enumMap.put("是", "1");
    }
    
    private String itsmCode;
    private ARepositoryFactory repositoryFactory;
    
    public BaseDataImporter(String itsmCode, ARepositoryFactory repositoryFactory) {
        this.itsmCode = itsmCode;
        this.repositoryFactory = repositoryFactory;
    }
    
    public void importProjectEngineer(InputStream input) throws Exception {
        BufferedReader reader = null;
        try {
            Map<String, String> row = new HashMap<>();
            reader = new BufferedReader(new InputStreamReader(input, "GB2312"));

            // read first line that is title of columns
            String line = reader.readLine();
            if (CommonUtil.isNullOrEmpty(line))
                throw new Exception("file is empty");

            ItsmEntityTranslator translator = new ItsmEntityTranslator();
            while ((line = reader.readLine()) != null) {
                String[] fields = line.split(",");
                row.clear();
                row.put("chrloginname", fields[1]);
                row.put("servicetype", enumMap.get(fields[2]));
                row.put("suppliernumber", fields[3]);
                row.put("chrmobilenumber", fields[5]);
                row.put("enginertype", enumMap.get(fields[6]));
                row.put("status", enumMap.get(fields[7]));
                row.put("openorder", fields[8]);
                row.put("provincename", fields[9]);
                row.put("cityname", fields[10]);
                row.put("stationadmin", enumMap.get(fields[11]));
                row.put("county", fields[13]);
                row.put("projectid", fields[14]);
                row.put("projectname", fields[15]);
                row.put("chrusername", fields[16]);

                ProjectEngineerPO po = translator.translate(itsmCode, EntityName.PROJECTENGINEER, row);
                po.setId("PE" + (po.getProjectId() + po.getEngineerId()).hashCode());

                po.getProject().setId(po.getProjectId());
                po.getEngineer().setId(po.getEngineerId());
                po.getEngineer().setLoginName(po.getEngineerId());

                repositoryFactory.getEngineerRepository().storeSyncProjectEngineer(itsmCode, po);
            }
        }
        finally {
            CommonUtil.closeIO(reader);
        }
    }
}
